import request from '@/utils/request'

const api = {
  user: '/user/list',
  role: '/role/list/page',
  service: '/service',
  permission: '/resource/list/page',
  orgTree: '/org/tree'
}

export default api

export function getUserList(parameter) {
  return request({
    url: api.user,
    method: 'post',
    data: parameter
  })
}

export function getServiceList(parameter) {
  return request({
    url: api.service,
    method: 'get',
    params: parameter
  })
}

export function getPermissions(parameter) {
  return request({
    url: api.permission,
    method: 'post',
    data: parameter
  })
}

export function getOrgTree(parameter) {
  return request({
    url: api.orgTree,
    method: 'get',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService(parameter) {
  return request({
    url: api.service,
    method: parameter.id === 0 ? 'post' : 'put',
    data: parameter
  })
}

export function saveSub(sub) {
  return request({
    url: '/sub',
    method: sub.id === 0 ? 'post' : 'put',
    data: sub
  })
}

/**
 * 校验唯一性
 *
 * @param tableName 表名称
 * @param key       校验字段
 * @param values    参数值
 * @returns {AxiosPromise}
 */
export function exist(tableName, key, values) {
  return request({
    url: `/uniqueness/onlyOne/${ tableName }/${ key }/${ values }`,
    method: 'get'
  })
}
